/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package hirt;

import java.nio.ByteBuffer;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Actions are the verbs of this program
 * they are used to tell each of the other player (and the
 * server) what everyone is doing
 * They are passed along with message across the network
 * @author zamille
 */
public class Action
{
    /** The time it takes this action to complete*/
    protected double durationTime;
    /** The time that the action begins*/
    protected double startTime;
    /** The item preformning the action*/
    protected Item actor;
    /** The type of message this action will be used with*/
    protected byte messageType;

    /**
     *
     */
    public Action()
    {
        actor = null;
        startTime = 0;
        durationTime = 0;
    }

    /**
     *
     * @param theActor
     */
    public Action(Item theActor)
    {
        actor = theActor;
        startTime = 0;
        durationTime = 0;
    }

    /**
     *
     * @param theActor
     * @param start Time
     * @param duration Time
     */
    public Action(Item theActor, double start, double duration)
    {
        actor = theActor;
        startTime = start;
        durationTime = duration;
    }

    /**
     * Builds an Action from a byte stream, presumably created by an action across the network
     * this action should be identical to that one
     * @param bytes
     * @param items: Master Item List used to build the action
     */
    public Action(ByteBuffer bytes, Vector<Item> items)
    {
        fromBytes(bytes,items);
    }

    /**
     * Builds an Action from a save string, presumably created by an action when saving to a file
     * this action should be identical to that one
     * @param saveString
     * @param items: Master Item List used to build the action
     */
    public Action(String saveString, Vector<Item> items)
    {
        fromSaveString(saveString,items);
    }

    /**
     * Builds an Action from a byte stream, presumably created by an action across the network
     * this action should be identical to that one
     * @param bytes
     * @param items: Master Item List used to build the action
     * @return what's left of the parem bytes after this methods completes
     */
    protected ByteBuffer fromBytes(ByteBuffer bytes, Vector<Item> items)
    {
        actor = ByteChanger.getItem(bytes, items, 0);
        startTime = bytes.getDouble(4);
        durationTime = bytes.getDouble(12);
        try {
            bytes = ByteChanger.truncate(bytes, 20);
        } catch (Exception ex) {
            
        }
        return bytes;
    }

    public Item getActor()
    {
        return actor;
    }

    public void setActor(Item actor)
    {
        this.actor = actor;
    }

    public double getDurationTime()
    {
        return durationTime;
    }

    public void setDurationTime(double durationTime)
    {
        if (durationTime >=0)
            this.durationTime = durationTime;
    }

    public double getStartTime()
    {
        return startTime;
    }

    public void setStartTime(double startTime)
    {
        if (startTime >= 0)
            this.startTime = startTime;
    }

    /**
     * Returns a ByteBuffer representation of this Action
     * Used to send actions over the network
     * @return ByteBuffer
     */
    public ByteBuffer toByte()
    {
        ByteBuffer buf = ByteBuffer.allocate(74);
        buf.putInt(ByteChanger.itemToByte(actor));
        buf.putDouble(startTime);
        buf.putDouble(durationTime);
        return buf;
    }

    /**
     * Returns a String representation of this Action
     * Used to save this action to a file
     * @return Save String
     */
    public String toSaveString()
    {
        String text = "";
        text+= actor + "\t";
        text+= startTime + "\t";
        text+= durationTime + "\t";
        return text;
    }

    /**
     * Builds an Action from a save string, presumably read from a save
     * this action should be identical to the one saved in the file
     * @param save string
     * @param items: Master Item List used to build the action
     * @return what's left of the parem SaveString after this methods completes
     */
    protected String fromSaveString(String text, Vector<Item> items)
    {
        String substring = ByteChanger.getNextValue(text);
        actor = ByteChanger.getItem(substring, items);

        text = ByteChanger.incrementString(text);
        substring = ByteChanger.getNextValue(text);
        startTime = Double.parseDouble(substring);

        text = ByteChanger.incrementString(text);
        substring = ByteChanger.getNextValue(text);
        durationTime = Double.parseDouble(substring);

        text = ByteChanger.incrementString(text);
        return text;
    }



    public byte getMessageType()
    {
        return messageType;
    }
}
